package com.qidian.buy.cms.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qidian.buy.cms.entity.ContentCategory;
import com.qidian.buy.cms.mapper.ContentCategoryMapper;
import com.qidian.buy.cms.service.IContentCategoryService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qidian.buy.util.pojo.QdResult;
import org.springframework.aop.framework.AopContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 戴着假发的程序员
 * @since 2024-05-06
 */
@Service
public class ContentCategoryServiceImpl extends ServiceImpl<ContentCategoryMapper, ContentCategory> implements IContentCategoryService {
    @Autowired
    private ContentCategoryMapper contentCategoryMapper;
    @Override
    public QdResult<IPage<ContentCategory>> queryByParentIdAndPage(Long parentId, Integer page, Integer size) {
        IPage<ContentCategory> pageInfo = new Page<>(page,size);
        QueryWrapper<ContentCategory> wrapper = new QueryWrapper<>();
        wrapper.eq("parent_id",parentId);
        pageInfo = contentCategoryMapper.selectPage(pageInfo,wrapper);
        return QdResult.success(pageInfo);
    }

    @Override
    public QdResult saveContentCat(ContentCategory category) {
        // 判断是否传递了parentId
        if(category.getParentId() == null){
            category.setParentId(0l);// 如果是null就设置为0;
        }
        QueryWrapper<ContentCategory> wrapper = new QueryWrapper<>();
        wrapper.eq(category != null && category.getCatName() != null ,"cat_name",category.getCatName());
        if (contentCategoryMapper.selectCount(wrapper) > 0) {
            return QdResult.duplicate("该分类已存在");
        }
        wrapper = new QueryWrapper<>();
        wrapper.eq(category != null && category.getCatMark() != null ,"cat_mark",category.getCatMark());
        if (contentCategoryMapper.selectCount(wrapper) > 0) {
            return QdResult.duplicate("该分类标识已存在");
        }
        int result = contentCategoryMapper.insert(category);
        if(result == 1 &&  !category.getParentId().equals(new Long(0))){
            // 修改它的父节点的parent值为1
            UpdateWrapper<ContentCategory> updateWrapper = new UpdateWrapper<>();
            updateWrapper.set("parent",1);
            updateWrapper.eq("cat_Id",category.getParentId());
            contentCategoryMapper.update(updateWrapper);
        }
        return QdResult.isSuccess(result);
    }

    @Override
    public QdResult<List<ContentCategory>> queryAll() {
        IContentCategoryService contentCategoryService = (IContentCategoryService) AopContext.currentProxy();
        return QdResult.success(contentCategoryService.queryByParentId(0L));
    }

    @Override
    public List<ContentCategory> queryByParentId(Long parentId) {
        QueryWrapper<ContentCategory> wrapper = new QueryWrapper<>();
        wrapper.eq("parent_id",parentId);
        List<ContentCategory> contentCategories = contentCategoryMapper.selectList(wrapper);
        if(contentCategories == null || contentCategories.size() == 0){
            return null;
        }
        IContentCategoryService contentCategoryService = (IContentCategoryService) AopContext.currentProxy();
        for (ContentCategory cat : contentCategories){
            // 递归
            cat.setChildren(contentCategoryService.queryByParentId(cat.getCatId()));
        }
        return contentCategories;
    }
}
